package com.sjhua.life.Clock.View;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Time;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.sjhua.life.Clock.R;
import com.sjhua.life.Clock.Service.SimpleService;

public class MainActivity extends AppCompatActivity {
	TextView date;
	TextView time;

	// handler类接收数据
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {

			if (msg.what == 1) {
				updateui();
				new Thread(new get_debug_info(1)).start();
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//设置全屏显示
		setFullscreen(false, false);
		setNavigationStatusColor(0x000000);
		setContentView(R.layout.activity_main);

		final Intent intent = new Intent(this, SimpleService.class);
		final Intent intent1 = new Intent(this, BindActivity.class);
		final Intent intent2 = new Intent(this, MessengerActivity.class);

		date = (TextView) findViewById(R.id.textViewyyyymmdd);
		time = (TextView) findViewById(R.id.textViewhhmmss);
		updateui();

		new Thread(new get_debug_info(1)).start();

		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				startService(intent);
			}
		});

		Button button2 = (Button) findViewById(R.id.button2);
		button2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				stopService(intent);
			}
		});

		Button button3 = (Button) findViewById(R.id.button3);
		button3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				startActivity(intent1);
			}
		});

		Button button4 = (Button) findViewById(R.id.button4);
		button4.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(intent2);
			}
		});
	}



	class get_debug_info implements Runnable {
		private int timeout = 10;
		public get_debug_info(int timeout){this.timeout = timeout;}


		@Override
		public void run() {
			while (timeout > 0) {
				try {
					Thread.sleep(1000);         //sleep 1 second
					timeout--;
					if (timeout == 0) {
						Message msg = new Message();
						msg.what = 1;
						handler.sendMessage(msg);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}
	}

	public void updateui(){
		Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
		t.setToNow(); // 取得系统时间。
		int year = t.year;
		int month = t.month + 1;
		int day = t.monthDay;
		int hour = t.hour; // 0-23
		int minute = t.minute;
		int second = t.second;
		String str_date = String.format("%04d年%02d月%02d日", year, month, day);
		String str_time = String.format("%02d:%02d:%02d", hour, minute, second);

		date.setText(str_date);
		time.setText(str_time);
	}

	public void setFullscreen(boolean isShowStatusBar, boolean isShowNavigationBar) {
		int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
				| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
				| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

		if (!isShowStatusBar) {
			uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
		}
		if (!isShowNavigationBar) {
			uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
		}
		getWindow().getDecorView().setSystemUiVisibility(uiOptions);

		//隐藏标题栏
		getSupportActionBar().hide();
		//专门设置一下状态栏导航栏背景颜色为透明，凸显效果。
		setNavigationStatusColor(Color.TRANSPARENT);
	}

	public void setNavigationStatusColor(int color) {
		//VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
		if (Build.VERSION.SDK_INT >= 21) {
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
			getWindow().setNavigationBarColor(color);
			getWindow().setStatusBarColor(color);
		}
	}
}
